package com.poisonh.unifiedauth.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.RegexUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

/**
 * 新手机号
 * Created by PoisonH on 2016/6/28.
 */
public class InputNewPhoneActivity extends BaseActivity implements View.OnClickListener
{

    private Button mBtnNextPhone;
    private EditText mEtNewPhone;
    private String mStrNewPhone;

    @Override

    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_inputnewphone);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        setTitleTxt("新手机号");
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);

        mEtNewPhone = (EditText) findViewById(R.id.et_inputnewhone);
        mBtnNextPhone = (Button) findViewById(R.id.btn_nextphone);
        mBtnNextPhone.setOnClickListener(this);
        mEtNewPhone.addTextChangedListener(mEditTextPhoneWatcher);

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
            case R.id.btn_nextphone:
                mStrNewPhone = mEtNewPhone.getText().toString();
                mBtnNextPhone.setEnabled(false);
                if (mStrNewPhone.trim().length() != 11)
                {
                    mBtnNextPhone.setEnabled(true);
                    ToastUtils.showToast(this, "请输入11位手机号");
                } else
                {
                    if (mStrUserPhone.equals(mStrNewPhone))
                    {
                        mBtnNextPhone.setEnabled(true);
                        ToastUtils.showToast(this, "亲，您的号码已经绑定了哦!");
                    } else
                    {

                        checkPhoneRegistry(mStrNewPhone);
                    }
                }
                break;

        }
    }

    /**
     * 检查手机号是否注册
     *
     * @param phone
     */
    private void checkPhoneRegistry(String phone)
    {

        HttpParams params = new HttpParams();
        params.put("phone", phone);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.ISREGISTRY, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                mBtnNextPhone.setEnabled(true);
                Intent mIntent = new Intent(InputNewPhoneActivity.this, ChangePhoneActivity.class);
                mIntent.putExtra("PHONE", mStrNewPhone);
                startActivity(mIntent);
                finish();
            }

            @Override
            public void onFailure(String failReason)
            {
                mBtnNextPhone.setEnabled(true);
                ToastUtils.showToast(InputNewPhoneActivity.this, failReason);
            }
        });
    }

    TextWatcher mEditTextPhoneWatcher = new TextWatcher()
    {
        private CharSequence temp;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (temp.length() == 11)
            {
                mBtnNextPhone.setEnabled(true);
                mBtnNextPhone.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg));
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mEtNewPhone.getWindowToken(), 0);
            } else
            {
                mBtnNextPhone.setEnabled(true);
                mBtnNextPhone.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg_));
            }
        }
    };
}
